<?php

namespace app\shop\controller;

use think\Request;
use think\Exception;
use excel\ExcelReader;
use think\Db;
use app\crm\model\Sn;
use app\shop\model\Linkman as LinkmanModel;
use app\shop\model\Client as ClientModel;
use app\shop\model\Clientbill as ClientbillModel;
use app\shop\model\ClientCategory as ClientCategoryModel;
use app\shop\model\StaffTag as StaffTagModel;
use app\shop\model\ClientLog as ClientLogModel;
use app\crm\model\Staff as StaffModel;
use app\shop\model\ClientStaff as ClientStaffModel;
use app\shop\model\StaffLog as StaffLogModel;
class Client extends \ShopController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $clientModel = new ClientModel();

        try {
            $params = $request->get();

            $data = $clientModel->search($params);


            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function simpleClient(Request $request)
    {
        $clientModel = new ClientModel();

        try {
            $params = $request->get();

            $data = $clientModel->simpleClient($params);


            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function simpleLinkman(Request $request)
    {
        $clientModel = new ClientModel();

        try {
            $params = $request->get();

            $data = $clientModel->simpleLinkman($params);


            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function getClientSearchData(Request $request){
        $clientModel = new ClientModel();

        try {
            $params = $request->post();

            $data = $clientModel->getClientSearchData($params);


            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function getStaffSearchData(Request $request){
        $clientModel = new ClientModel();

        try {
            $params = $request->post();

            $data = $clientModel->getStaffSearchData($params);


            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function getTrade(){
        $clientModel = new ClientModel();

        try {

            $data = $clientModel->getTrade();

            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 联系人列表
     */
    public function linkman(Request $request){
        $clientModel = new ClientModel();

        try {
            $params = $request->get();

            $data = $clientModel->searchStaff($params);


            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 客户详情
     */
    public function clientDetail($id){
        $clientModel = new ClientModel();
        try {
            $data = $clientModel->getClientDetail($id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 联系人详情
     */
    public function staffDetail($id){
        $clientModel = new ClientModel();
        try {
            $data = $clientModel->getStaffDetail($id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 直接新增客户
     */
    public function addClient(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();

            
            $data = $clientModel->addClient($params);
            

            $this->successTerminate($data);
        } catch (Exception $e) {
            
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    
    /**
     * 关联现有客户
     */
    public function relationClient(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();

            
            $data = $clientModel->relationClient($params);
            $this->successTerminate($data);
            

        } catch (Exception $e) {
           
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 直接新增联系人
     */
    public function addStaff(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();
            $data = $clientModel->addStaff($params);
            $this->successTerminate($data);

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 关联现有联系人
     */
    public function relationStaff(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();

           
            $data = $clientModel->relationStaff($params);
            $this->successTerminate($data);
           

        } catch (Exception $e) {
            
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    
    //获取客户修改数据
    public function getClientUpdateInfo($id){
        $clientModel = new ClientModel();
        try {
            $data = $clientModel->getClientUpdateInfo($id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //获取联系人修改数据
    public function getStaffUpdateInfo($id){
        $clientModel = new ClientModel();
        try {
            $data = $clientModel->getStaffUpdateInfo($id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //修改客户
    public function updateClient(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();

            
            $data = $clientModel->updateClient($params);
            $this->successTerminate($data);
            

        } catch (Exception $e) {
            
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //修改联系人
    public function updateStaff(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();


            $data = $clientModel->updateStaff($params);
            $this->successTerminate($data);


        } catch (Exception $e) {

            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //删除客户
    public function delClient($id){
        $clientModel = new ClientModel();
        try {
            $data = $clientModel->delClient($id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    
    //批量删除客户
    public function batchDel(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();
            $data = $clientModel->batchDel($params['client']);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //全部删除客户
    public function delAll(){

        set_time_limit(0);

        $clientModel = new ClientModel();
        try {
            $data = $clientModel->delAll();
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
        
    }

    //删除联系人
    public function delStaff($id){
        $clientModel = new ClientModel();
        try {
            $data = $clientModel->delStaff($id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //批量删除联系人
    public function batchDelStaff(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();
            $data = $clientModel->batchDelStaff($params['staff']);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //全部删除联系人
    public function delAllStaff(){

        set_time_limit(0);

        $clientModel = new ClientModel();
        try {
            $data = $clientModel->delAllStaff();
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
        
    }

    //解除关联(客户-联系人)
    public function removeRelevance(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();
            $data = $clientModel->removeRelevance($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //修改订货人
    public function changeOrderer(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();
            $data = $clientModel->changeOrderer($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //关联现有客户(客户-客户)
    public function clientRelevance(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();
            $data = $clientModel->clientRelevance($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //解除关联客户
    public function removeclient(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();
            $data = $clientModel->removeclient($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //客户关联客户
    public function clientRelevanceAdd(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();


            $data = $clientModel->clientRelevanceAdd($params);


            $this->successTerminate($data);
        } catch (Exception $e) {

            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //修改分类
    public function editCategory(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();


            $data = $clientModel->editCategory($params);
            $this->successTerminate($data);


        } catch (Exception $e) {

            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //修改标签
    public function editTag(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();

            $data = $clientModel->editTag($params);
            $this->successTerminate($data);

        } catch (Exception $e) {

            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //修改销售区域
    public function editRegion(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();


            $data = $clientModel->editRegion($params);
            $this->successTerminate($data);


        } catch (Exception $e) {

            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //修改创建人
    public function editCreateStaff(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();


            $data = $clientModel->editCreateStaff($params);
            $this->successTerminate($data);


        } catch (Exception $e) {

            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function getName($id)
    {
        $ClientModel = new ClientModel();
        $name = $ClientModel->where('client_id',$id)->field('client_name')->find();
        return $name;
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $ClientModel = new ClientModel();
        try {

            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $clientModel = new ClientModel();

        $params = $request->post();

        $validate = \think\Loader::validate('Client');
        if ( !$validate->scene('')->check($params) ) {
            return $validate->getError();
        }

        try {

            $flag = $clientModel->where('client_name',$params['client_name'])->value('client_id');
            if($flag){
                $this->serverErrorTerminate('客户名称已存在');
            }

            $utid = \request()->header('userId');
            $params['area'] = !empty($params['area']) ? implode(',', $params['area']) : null;
            $params['create_staff_id'] = $utid;
            $params['create_staff'] = Db::name('crm_staff')->where('staff_id',$utid)->value('staff_name');
            $params['update_staff'] = $params['create_staff'];
            $params['last_contract_staff'] = $params['create_staff'];
            $params['create_at'] = date('Y-m-d H:i:s');
            $params['update_at'] = date('Y-m-d H:i:s');
            $params['last_contract_time'] = date('Y-m-d H:i:s');
            
            $clientModel->data($params)->allowField(true)->save();
            $params['client_id'] = $clientModel->client_id;

            //保存客户操作日志
            $ClientLogModel = new ClientLogModel();
            $ClientLogDb = $ClientLogModel->ClientLogDb();
            $log = [
                'client_id' => $params['client_id'],
                'operater' => $params['create_staff'],
                'content' => '创建客户'
            ];
            $ClientLogDb->insert($log);

            //保存客户分类
            $clientCategoryModel = new ClientCategoryModel();
            $clientCategoryDb = $clientCategoryModel->ClientCategoryDb();
            foreach($params['category'] as $v){
                $tmp = ['client_id'=>$params['client_id'],'client_cate_id'=>$v];
                $clientCategoryDb->insert($tmp);
            }

            //保存客户财务
            $ClientbillModel = new ClientbillModel();
            $ClientbillModel->data($params)->allowField(true)->save();

            //处理联系人相关
            $StaffModel = new StaffModel();
            $ClientStaffModel = new ClientStaffModel();
            $ClientStaffDb = $ClientStaffModel->ClientStaffDb();
            $StaffLogModel = new StaffLogModel();
            $StaffLogDb = $StaffLogModel->StaffLogDb();
            $StaffTagModel = new StaffTagModel();
            //直接新增的联系人
            if(!empty($params['add_linkman'])){
                foreach($params['add_linkman'] as &$v){
                    //新增联系人
                    $v['create_staff'] = $params['create_staff'];
                    $v['update_staff'] = $params['create_staff'];
                    $v['last_contract_staff'] = $params['create_staff'];
                    $v['create_at'] = date('Y-m-d H:i:s');
                    $v['update_at'] = date('Y-m-d H:i:s');
                    
                    //获取staff_id
                    $SN  = new Sn();
                    $snS = $SN->getSn('YG');
                    $v['staff_id'] = $snS['0'];

                     //登录账号
                    $v['user_name'] = $v['staff_name'];
                    $v['salt'] = $this->generateSalt();
                    $v['password'] = md5(md5('123456').$v['salt']);
    
                    $StaffModel->data($v)->allowField(true)->save();
                    $staff_id = $snS['0'];
                    //添加关联
                    $tmp = ['client_id'=>$params['client_id'],'staff_id'=>$staff_id];
                    $ClientStaffDb->insert($tmp);

                    //标签
                    if(!empty($v['tag'])){
                        foreach($v['tag'] as $t){
                            $tag_tmp = [
                                'tag_id' => $t,
                                'staff_id' => $staff_id
                            ];
                            $StaffTagModel->insert($tag_tmp);
                        }   
                    }

                    //添加日志
                    $log = [
                        'staff_id' => $staff_id,
                        'operater' => $params['create_staff'],
                        'content' => '创建联系人'
                    ];
                    $StaffLogDb->insert($log);
                }
            }
            
            if(!empty($params['relation_linkman'])){
                //关联已有的联系人
                foreach($params['relation_linkman'] as &$v){
                    //添加关联
                    $tmp = ['client_id'=>$params['client_id'],'staff_id'=>$v['staff_id']];
                    $ClientStaffDb->insert($tmp);
                }
            }
            

            $this->successTerminate();

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //列表新增联系人
    public function saveLinkman(Request $request){
        $LinkmanModel = new LinkmanModel();
        try {
            $params = $request->post();

            $data = $LinkmanModel->saveLinkman($params);
            $this->successTerminate($data);

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 获取简单数据
     */
    public function getSimpleClient(){
        try {
            $ClientModel = new ClientModel();

            $data = $ClientModel->field('client_id,client_name,level_id')->select();

            $this->successTerminate($data);

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //通过客户ID查询发票信息
    public function getBill(Request $request){
        try {
            $client_id = $request->get('client_id');
            $ClientbillModel = new ClientbillModel();
            $data = $ClientbillModel->where('client_id',$client_id)->find();
            $this->successTerminate($data);

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }


    }

    //通过客户ID修改发票信息
    public function updateBill(Request $request){
        try {
            $params = $request->post();
            $ClientbillModel = new ClientbillModel();
            $data = $ClientbillModel->where('client_id',$params['client_id'])->update($params);
            $this->successTerminate($data);

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 上传图片
     * @return \think\response\Json
     */
    public function uploadImage(Request $request)

    {
        // 获取表单上传文件
        $file = request()->file('image');
        $id = $request->post('id');
        /* @var $file \Think\File */
        $info = $file->validate([
            'size' => 1024 * 1024 * 2,
            'ext' => 'jpg,jpeg,png',
        ])->move(GOODS_IMAGE_SAVE_PATH);

        if ($info) {
            $url = GOODS_IMAGE_URL . $info->getSaveName();

            $ClientModel = new ClientModel();
            $img_id = $ClientModel->saveImage($id,$url);

            $this->successTerminate($img_id);
        } else {
            $this->serverErrorTerminate($file->getError());
        }
    }

    //解除关联图片
    public function removeImage(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->get();
            $data = $clientModel->removeImage($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 上传附件
     * @return \think\response\Json
     */
    public function uploadAccessory(Request $request)

    {
        // 获取表单上传文件
        $file = request()->file('accessory');
        $id = $request->post('id');
        /* @var $file \Think\File */
        $info = $file->validate([
            'size' => 1024 * 1024 * 2,
            'ext' => 'pdf,word,excel,txt,jpg,png,bmp,gif,rar,zip',
        ])->move(GOODS_ACCESSORY_SAVE_PATH);

        if ($info) {
           $params =  [
                'url'=>GOODS_ACCESSORY_URL . $info->getSaveName(),
                'name'=>$file->getInfo('name'),
            ];

            $ClientModel = new ClientModel();
            $accessory_id = $ClientModel->saveAccessory($id,$params);

            $this->successTerminate($accessory_id);
        } else {
            // 上传失败获取错误信息
            $this->serverErrorTerminate($file->getError());
        }
    }

    //解除关联附件
    public function removeAccessory(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->get();
            $data = $clientModel->removeAccessory($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function importClient(){

        set_time_limit(0);

        $file = request()->file('accessory');

        $info = $file->validate([
            'size' => 1024 * 1024 * 2,
            'ext' => 'xls,xlsx',
        ])->move(GOODS_ACCESSORY_SAVE_PATH);
        if ($info) {

            $client_file = ROOT_PATH.'public/accessory/'.$info->getSaveName();
            $reader = new ExcelReader($client_file);
            $titlesMap = [
                //主数据
                'client_name' => '客户名称',
                'level' => '客户等级',
                'category' => '客户分类',
                'region' => '销售区域',
                'business_type' => '业务模式',
                'source' => '客户来源',
                'establish_time' => '成立时间',
                'service_rate' => '服务满意度',
                'deal_impression' => '交易印象',
                'mnemonic' => '助记码',
                'keywords' => '关键词',
                'phone' => '座机',
                'fax' => '传真',
                'email' => '邮箱',
                'website' => '官网',
                'micro_blog' => '微博',
                'address' => '地址',
                'main_product' => '主营产品',
                'enterprise_nature' => '企业性质',
                'year_turnover' => '年营业额',
                'enterprise_scale' => '企业规模',
                //发票数据
                'invoice' => '发票抬头',
                'tax_num' => '纳税人识别号',
                'invoice_address' => '发票地址',
                'bank_name' => '开户名称',
                'bank' => '开户银行',
                'bank_account' => '开户账号',
                'remark' => '发票备注',
                //联系人数据
                'staff_name' => '联系人姓名',
                'staff_duty' => '联系人职务',
                'staff_phone' => '联系人手机号',
                'staff_email' => '联系人邮箱',
                'staff_special_plane' => '联系人座机',
                'staff_qq' => '联系人QQ'
            ];

            $data = $reader->exec($titlesMap);

            $clientModel = new ClientModel();

            try {

                $error = $clientModel->importClient($data,$cover);

                $this->successTerminate($error);

            } catch (Exception $e) {

                $this->serverErrorTerminate($e->getMessage());

            }

            $this->successTerminate($data);
         } else {
             // 上传失败获取错误信息
             $this->serverErrorTerminate($file->getError());
         }
    }

    public function importStaff(){

        set_time_limit(0);

        $file = request()->file('accessory');
        
        $info = $file->validate([
            'size' => 1024 * 1024 * 2,
            'ext' => 'xls,xlsx',
        ])->move(GOODS_ACCESSORY_SAVE_PATH);
        if ($info) {

            $staff_file = ROOT_PATH.'public/accessory/'.$info->getSaveName();
            $reader = new ExcelReader($staff_file);
            $titlesMap = [
                'staff_name' => '姓名',
                'phone' => '手机',
                'tag' => '联系人标签',
                'duty' => '职务',
                'birthday' => '生日',
                'sex' => '性别',
                'special_plane' => '座机',
                'email' => '邮箱',
                'wechat' => '微信',
                'micro_blog' => '微博',
                'qq' => 'QQ',
                'native_place' => '籍贯',
                'hobby' => '兴趣爱好',
                'education_background' => '教育背景',
                'client_name' => '客户名称'
            ];

            $data = $reader->exec($titlesMap);

            $StaffModel = new StaffModel();

            try {

                $error = $StaffModel->importStaff($data);

                $this->successTerminate($error);

            } catch (Exception $e) {

                $this->serverErrorTerminate($e->getMessage());

            }

            $this->successTerminate($data);
         } else {
             // 上传失败获取错误信息
             $this->serverErrorTerminate($file->getError());
         }
    }

    public function recycleClient(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->get();
            $data = $clientModel->recycleClient($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function clientRemove(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();
            $data = $clientModel->clientRemove($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function clientRecover(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();
            $data = $clientModel->clientRecover($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function clientTruncate(){
        $clientModel = new ClientModel();
        try {
            $data = $clientModel->clientTruncate();
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function recycleStaff(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->get();
            $data = $clientModel->recycleStaff($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function staffRemove(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();
            $data = $clientModel->staffRemove($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function staffRecover(Request $request){
        $clientModel = new ClientModel();
        try {
            $params = $request->post();
            $data = $clientModel->staffRecover($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function staffTruncate(){
        $clientModel = new ClientModel();
        try {
            $data = $clientModel->staffTruncate();
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 生成密码盐
     */
    public function generateSalt()
    {
        $str = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
        $strMaxIndex = strlen($str) - 1;
        $salt = '';
        $saltLenght = 6;
        for ($i = 0; $i < $saltLenght; $i ++) {
            $salt .= $str[mt_rand(0, $strMaxIndex)];
        }
        return $salt;
    }

}